package com.zhihao.takeaway.controller;

import com.zhihao.takeaway.common.utils.JwtUtil;
import com.zhihao.takeaway.common.utils.MD5Utils;
import com.zhihao.takeaway.common.utils.Result;
import com.zhihao.takeaway.entity.Employee;
import com.zhihao.takeaway.entity.Person;
import com.zhihao.takeaway.entity.User;
import com.zhihao.takeaway.mapper.TableMapper;
import com.zhihao.takeaway.service.EmployeeService;
import com.zhihao.takeaway.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
public class loginAndEnrollController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private UserService userService;
    @Autowired
    private TableMapper tableMapper;


    @PostMapping("/login")
    public Result login(String account, String password, HttpSession session){
        Result result = new Result();
//员工登录
        if(account.substring(0,2).equals("YG")||account.equals("admin"))
        {
            Employee employee = employeeService.isEmployeeExist(account);
            if(employee == null){
                result.fail("该员工不存在");
            }else {
                if (employee.getStatus() == 0) {
                    result.fail("该员工目前账号已被查封");
                } else {
                    if (!employee.getPassword().equals(MD5Utils.inputPassToFormPass(password))) {
                        result.fail("密码不正确，请重新输入");
                    }else{
                        result.success("登陆成功");
                        session.setAttribute("nowAccount",employee.getAccount());
                        Map<String,Object> map = new HashMap<>();
                        map.put("employee",employee);
                        map.put("token", JwtUtil.generateToken(employee.getId()));
                        result.setData(map);
                    }
                }
            }
        }else{
            User user = userService.isUserExist(account);
            if(user == null){
                result.fail("该用户不存在");
            }else{
                if(user.getStatus() == 0){
                    result.fail("该用户目前账号已被查封");
                }else{
                    session.setAttribute("nowAccount",user.getAccount());
                    result.success("登陆成功");
                    Map<String,Object> map = new HashMap<>();
                    map.put("user",user);
                    map.put("token", JwtUtil.generateToken(user.getId()));
                    result.setData(map);
                }
            }
        }

        return result;
    }

    @PostMapping("/enroll")
    public Result enroll(@RequestBody Person person){
        Result result = new Result();
//        0---客户   1----员工
        if(person.getIsOther().equals("1")){
            Boolean flag = employeeService.isEnroll(person);
            if(flag){
                String account = employeeService.enroll(person);
                if(account == null){
                    result.fail("注册失败，请重新注册");
                }else {
                    result.success("注册成功");
                    tableMapper.createTable("record"+account);
                    result.setData(account);
                }
            }else{
                result.fail("注册失败，当前信息已被注册，请重新验证");
            }
        }else{
            Boolean flag = userService.isEnroll(person);
            if(flag){
                String account = userService.enroll(person);
                if(account == null){
                    result.fail("注册失败，请重新注册");
                }else {
                    result.success("注册成功");
                    result.setData(account);
                    tableMapper.createTable("record"+account);
                }
            }else{
                result.fail("注册失败，当前信息已被注册，请重新验证");
            }
        }
        return result;
    }
}
